package com.pkche.core.advanced.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

public class AudioUtils {

	private static final Logger log = Logger.getLogger(AudioUtils.class); 
	
	/**
	 * 使用ffmpeg将arm转换为mp3
	 * @param sourceFileAmr
	 * @param toFileMp3
	 */
	public static void amrTomp3(String sourceFileAmr,String toFileMp3){
		try{	

			log.debug("**************  将amr转换为mp3  sourceFile : "+ sourceFileAmr+"   toFileMp3: "+toFileMp3+"****************"); 

			Runtime   rt   =   Runtime.getRuntime();
			String  command   =   "ffmpeg -i " + sourceFileAmr + " " + toFileMp3; 

			log.debug("ffmpeg exec command = " + command); 

			Process proc = rt.exec(command); 
			InputStream stderr = proc.getErrorStream(); 
			InputStreamReader isr = new InputStreamReader(stderr); 
			BufferedReader br = new BufferedReader(isr); 
			String line = null; 
			StringBuffer sb = new StringBuffer(); 
			while ((line = br.readLine()) != null) 
			sb.append(line); 

			log.debug("ffmpeg Process errorInfo: " + sb.toString()); 

			int exitVal = proc.waitFor(); 
			log.debug("ffmpeg Process exitValue: " + exitVal); 

			} 
			catch(Exception e) 
			{ 
			log.debug("ffmpeg exec cmd Exception " + e.toString()); 
			} 
	}
	
}
